home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.netins.net!isac!gg
- From: gg@isac.hces.com (Greg Goodrich)
- Subject: Re: Processing a structure- how?
- Message-ID: <1996Feb29.232302.5640@isac.hces.com>
- Organization: Health Care Expert Systems
- X-Newsreader: TIN [version 1.2 PL2]
- References: <DnDv98.Ko1@network.com>
- Date: Thu, 29 Feb 1996 23:23:02 GMT
-
- Mike Collins (collim@anubis.network.com) wrote:
- : Folks,
-
- : I want to write a generic routine that will print data contained
- : in structures on the screen (of a DOS-based PC, but I don't think that
- : comes into it). What I want is to be able to pass a structure or
- : structure pointer to the routine, which will then be able to find out what
- : the contents of the structure are, so that if I pass a customer structure
- : containing surname, forename, address, phone number, I will be able to
- : print this. If I pass a different structure containing catalog data, I
- : will be able to print Item, category, bin location, etc.
-
- You can make this happen, but it takes some coding. What you need is a
- mechanism to determine what the structure contains. You can set up
- something like this:
-
- struct {
- char *name;
- short type;
- short length;
- void *data;
- } DEFINITION;
-
- /* a sample array to be defined using above definition struct */
- typedef struct {
- char *mem1;
- int mem2;
- short mem3;
- char mem4;
- } arr1;
-
- #define T_SHORT 1
- #define T_INT 2
- #define T_CHAR 3
- #define T_STR 4
-
- DEFINITION darr1[] = {
- { "mem1", T_STR, sizeof(char*), offsetof(arr1, mem1) },
- { "mem2", T_INT, sizeof(char*), offsetof(arr1, mem1) },
- { "mem3", T_SHORT, sizeof(char*), offsetof(arr1, mem1) },
- { "mem4", T_CHAR, sizeof(char*), offsetof(arr1, mem1) },
- { "", 0, 0, 0 }
- };
-
-
- Now you can create an array of these structures, and initialize these
- structures with the proper data for each structure that you may pass in
- to a function. You then need only to look at the above structure for
- each of your structure members to determine all you need about the
- member to manipulate it. This can be very confusing, and therefore you
- should probably research greatly before attempting to code it. BTW, you
- need to use the macro offsetof() to determine where your structure
- members are relative to the beginning of the structure for the "data"
- pointer in the above structure definition. Good luck!
-
- Greg.
- --
- _______________________________________
- Greg Goodrich - gg@hces.com
- Software Engineer
- PACE Health Management Systems
-